package com.sumainfor.pc.epc.service.impl;


import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.CompuUtils;
import com.sumainfor.common.wechat.MapUtil;
import com.sumainfor.pc.epc.dao.ManagerWorkbenchMapper;
import com.sumainfor.pc.epc.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.epc.service.ManagerWorkbenchService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工程经理（工作台）
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-04-09 14:02
 **/
@Service
public class ManagerWorkbenchServiceImpl implements ManagerWorkbenchService {

    @Autowired
    ManagerWorkbenchMapper managerWorkbenchMapper;

    @Autowired
    ZzuulFeign zzuulFeign;

    @Autowired
    ZpcMktFeign zpcMktFeign;

    /**
    * <p>
    * 重要状态提醒
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult importantState(){
        JsonResult result = new JsonResult();
        Map<String,Object> params = managerWorkbenchMapper.getImportantState();
        return result.put(params);
    }

    /**
    * <p>
    * 采购进度
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult purchasingSchedule(){
        JsonResult result = new JsonResult();
        Map<String,Object> params = managerWorkbenchMapper.getPurchasingSchedule();


        JsonResult zpcMktResult = zpcMktFeign.getAuditHousingCount();
        Map<String,Object> zpcMktMap = (Map<String, Object>) zpcMktResult.get("result");
        params.put("InspectStateCount",zpcMktMap.get("InspectStateCount"));

        if (params == null||params.size()<1){
         result.putFailed("服务器繁忙...");
        }
        return result.put(params);
}

    /**
    * <p>
    * 装修进度
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult decorateProgress(){
        JsonResult result = new JsonResult();
        Map<String,Object> params = managerWorkbenchMapper.getDecorateProgress();
        if (params == null||params.size()<1){
            result.putFailed("服务器繁忙...");
        }
        return result.put(params);


    }
    /**
    * <p>
    * 供应商管理
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult supplierManagement(){
        JsonResult result = new JsonResult();
        Map<String,Object> params = managerWorkbenchMapper.getSupplierManagement();
        if (params == null||params.size()<1){
            result.putFailed("服务器繁忙...");

        }
        return result.put(params);

    }
    /**
    * <p>
    * 获取当月个人维修单数及其好评率情况
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult getRepairLists(){
        JsonResult result = new JsonResult();
        List<Map<String,Object>> param= managerWorkbenchMapper.getRepairLists();
        System.out.println("param"+param);
        Map<String,Object> params = new HashMap<>();
        if(param.size()>0){
            //保存维修师傅的编号
            List<String> SysUserIdList = new ArrayList<>();
            for (Map<String,Object> para:param) {
                if(para.get("SysUserId")!=null){
                    //添加为序师傅的编号
                    SysUserIdList.add(para.get("SysUserId").toString());
                   double count1=  (double) MapUtils.getInteger(para,"SysUserIdCount").intValue()*5;
                   double sum1 = (double)MapUtils.getInteger(para,"starSum").intValue();
                   para.put("Avg",CompuUtils.div(sum1,count1,2)*100);//求出评价星数的百分比;
                }

            }
             params.put("SysUserId",SysUserIdList);
            System.out.println("params"+params);
            //到网关区查询维修师傅的名字
            JsonResult zuulResult =zzuulFeign.getLoginName(params);
            List<Map<String,Object>> zuulList = (List<Map<String, Object>>) zuulResult.get("result");
            System.out.println("zuulList"+zuulList);
            if (zuulResult.get("errorCode").equals("0")) {
                for (Map<String, Object> map : param) {
                    for (Map<String, Object> map2 : zuulList) {
                        if (MapUtils.getString(map, "SysUserId") !=null&&MapUtils.getString(map2, "user_id")!=null&& MapUtils.getString(map, "SysUserId").equals(MapUtils.getString(map2, "user_id"))) {
                            map.putAll(map2);
                        }
                    }
                }
            }
            return result.put(param);
        }
       return result.put(new ArrayList<Map<String,Object>>());
    }

        /**
        * <p>
        * 当前月，季度，年的维修单情况
        * </p>
        *
        * @author: zhoucong
        * @since:
        */
        public  JsonResult getMaintenanceStatus(Map<String,Object> params){
              JsonResult result = new JsonResult();
              List<Map<String,Object>> paraList = managerWorkbenchMapper.getMaintenanceStatus(params);
            System.out.println("paraList"+paraList);
              Integer parInt = new Integer(0);
           //  List<Map<String,Object>> arrList = new ArrayList<>();
              Map<String,Object> countMap = new HashMap<>();
              if (paraList.size()>0){
                  for (Map<String,Object> para:paraList ) {
                      Integer par = MapUtils.getInteger(para,"RepArticleCount");
                      parInt = parInt+par;
                  }
                  countMap.put("sum",parInt);
                  System.out.println("countMap"+countMap);
                   paraList.add(countMap);
                  result.put(paraList);

                  return result;
              }else{
                 return  result.put(new ArrayList<Map<String,Object>>());
              }


        }
}
